﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Automation.Peers;
using System.Reflection;

namespace Gmantis.Controls.Automation
{
    public static class AutomationPeerFactory
    {
        public static AutomationPeer CreateAutomationPeer(object peerOwner, Type ownerType)
        {
            AutomationPeer peer = null;
            string[] strArray = ownerType.Assembly.FullName.Split(new char[] { '.' });
            strArray[0] = "Gmantis.Controls.Automation";
            Assembly assembly = null;
            string assemblyString = string.Join(",", strArray);

            try
            {
                assembly = Assembly.Load(assemblyString);
            }
            catch
            {
                return peer;
            }

            string name = string.Format("{0}.{1}AutomationPeer", strArray[0], ownerType.Name);
            Type type = assembly.GetType(name);
            if (type != null)
            {
                try
                {
                    peer = Activator.CreateInstance(type, new object[] { peerOwner }) as AutomationPeer;
                }
                catch { }
            }

            return peer;
        }
    }
}
